﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace LambdaToSql.EntityModel
{
    [Serializable]
    [DataContract]
    internal abstract class DeleteEntity<T> : BaseEntity<T> where T : class, new()
    {
        #region Base

        /// <summary>
        /// 条件
        /// </summary>
        [DataMember]
        public StringBuilder Where { get; set; }

        /// <summary>
        /// sql 参数
        /// </summary>
        [DataMember]
        public List<LambdaToSql.EntityModel.DataParameter> Parameters { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        [DataMember]
        public T Obj { get; set; }

        #endregion

        #region GetStr

        /// <summary>
        /// 最终Sql字符串 只读
        /// </summary>
        [DataMember]
        public string SqlStr
        {
            get
            {
                var sql = new StringBuilder();
                sql.Append("delete from");
                sql.Append(" " + this.TableName);
                sql.Append(" " + this.WhereStr);

                return sql.ToString();
            }
        }

        /// <summary>
        /// 条件字符串 只读
        /// </summary>
        [DataMember]
        public string WhereStr
        {
            get
            {
                var result = "";
                if (!string.IsNullOrEmpty(this.Where.ToString()))
                {
                    result = " where " + this.Where;
                }
                return result;
            }
        }

        #endregion


        public DeleteEntity(LambdaToSql.EntityModel.DbContext context) : base(context)
        {
            this.Parameters = new List<LambdaToSql.EntityModel.DataParameter>();
            this.Where = new StringBuilder();
        }
    }
}
